set more off
clear all

global DIR Dropbox\INE\data\bhh
global IPDIR Dropbox\INE\data\matchip
global INNODIR Dropbox\INE\data\Innovacion

cap log close
log using $DIR\cr_mktshare,t replace

********************************************************************************
*** File to construct market share for each firm at the 2-digit industry level
*** ENIA data only, by year.
*** BHH August 2019
********************************************************************************

run $DIR\fillin

use $DIR\enia_inno_ip_ex, clear
drop if totrev==.
drop if year>2005 | year<1995 /* remove non-ENIA obs */
sort id year
replace employ = . if employ==0
count if employ==.
fillin employ id year
count if employ==.

*** recode small ind categories
replace sic2d = 24 if sic2d==23
replace sic2d = 31 if sic2d==30

*** Recode sic2d to fold small industries into larger ones
egen x = mode(sic2d),by(id)
gen ind = int(x)
drop x
tab ind
replace ind = 15 if ind==16
replace ind = 24 if ind==23
replace ind = 31 if ind==30 | ind==32
label drop ilbl

label def ilbl 15 "food products and beverages, tobacco" 
label def ilbl 17 "textiles", add
label def ilbl 18 "wearing apparel; dressing and dyeing of fur", add
label def ilbl 19 "leather preparation & goods", add
label def ilbl 20 "wood, cork and straw products, ex furniture", add
label def ilbl 21 "paper and paper products", add
label def ilbl 22 "publishing, printing and reproduction of recorded media", add
label def ilbl 24 "chemicals and chemical products incl coke & refined oil", add
label def ilbl 25 "rubber and plastics products", add
label def ilbl 26 "other non-metallic mineral products", add
label def ilbl 27 "basic metals", add
label def ilbl 28 "fabricated metal products, except machinery and equipment", add
label def ilbl 29 "machinery and equipment n.e.c.", add
label def ilbl 31 "electrical & computing machinery, comm. equipment", add
label def ilbl 33 "medical, precision and optical instruments, watches and clocks", add
label def ilbl 34 "motor vehicles, trailers and semi-trailers", add
label def ilbl 35 "other transport equipment", add
label def ilbl 36 "furniture; manufacturing n.e.c.", add
label def ilbl 37 "recycling", add
label val ind ilbl
label var ind "Reduced 2-digit ISIC classes"

*** create rough market share information
keep totrev ind id year sic2d isic2 isic3

egen sumrev = sum(totrev),by(ind year)
egen nfirm = count(totrev),by(ind year)
label var nfirm "Number of firms in ind sector"
gen share = totrev/sumrev
label var share "Share of firm's sales in 2-digit industry"

egen sumrev4 = sum(totrev),by(isic3 year)
gen share4 = totrev/sumrev4
label var share4 "Share of firm's sales in 4-digit industry"

keep id year ind nfirm share share4 
sort ind year
tabstat share if ind~=ind[_n-1] | year~=year[_n-1],by(ind) stat(n mean)

do $DIR\cleanfile
save $DIR\mktshare,replace

log close



